<?php

/**
 * @param $target, $table   - Form's target: 
 *                            <form action='$target?table=$table'>
 * @param $submit_name, $submit_val - Submit button: 
 *                            <input type='submit' name='$submit_name' value='$submit_val'>
 * @param $showerr - TRUE: Displays error messages if an error occurs.
 *                   FALSE: Doesn't display error messages.
 
 * @output A Basic form in where each field is displayed in a seperate line as so:
 *         <field_name>: <input type="text">
 
 * @return - False if an error occurs, True otherwise.
 
 * @include tools/ColumnNames.php
 * 
 * @example of use:
 *     EchoTableForm("page.php","submit","Submit","tablename",TRUE)
 * Displays a form for all fields.
 * Directs to page.php?table=tablename when Submit is clicked
 */
function EchoTableForm($target, $submit_name, $submit_val, $table, $showerr) {
    $column = GetColumnNames($table, $showerr);

    $n = count($column);
    if($n == 0) return false;
    echo "\n<form action='" .$target."?table=".$table. "' method='post' class='form'>";
    echo "\n<table>";
    for($i=0; $i<$n; $i++) {
        echo "\n<tr>   <td width=150>".$column[$i]."</td>";
        echo "<td width=120><input type='text' name='".$column[$i]."' size='20'></td></tr>";
    }
    echo "\n</table>";
    echo "\n<input type='submit' name='".$submit_name."' value='".$submit_val."'>";
    echo "</form>";
    return true;
}

function EchoTableFormUpdate($target, $submit_name, $submit_val, $table, $showerr, $after) {
    $column = GetColumnNames($table, $showerr);

    $n = count($column);
    if($n == 0) return false;
    echo "\n<form action='" .$target."?table=".$table. "' method='post' class='form'>";
    echo "\n<table>";
    for($i=0; $i<$n; $i++) {
        echo "\n<tr>   <td width=150>".$column[$i]."</td>";
        echo "<td width=120><input type='text' name='".$column[$i]."' size='20'></td></tr>";
    }
    echo "\n</table>";
    echo "\n<input type='submit' name='".$submit_name."' value='".$submit_val."'>";
    echo $after;
    echo "</form>";
    return true;
}

function EchoHiddenForm($target, $table, $showerr){
   $column = GetColumnNames($table, $showerr);

    $n = count($column);
    if($n == 0) return false;
    echo "\n<form action='" .$target."?table=".$table. "' method='post' class='form'>";
    echo "\n<table>";
    for($i=0; $i<$n; $i++) {
        echo "\n<tr>   <td width=150>".$column[$i]."</td>";
        echo "<td width=120><input readonly='readonly' value='".$_POST[$column[$i]]."' name='".$column[$i]."' size='20'></td></tr>";
    }
    echo "\n</table>";
    echo "\n<input type='submit' name='submit' value='Delete'>";
    echo "</form>";
 
    return true;
    }

/**
 * @param $target - Form's target: 
 *                  <form action='$target'>
 * @param $submit_name, $submit_val - Submit button: 
 *                  <input type='submit' name='$submit_name' value='$submit_val'>
 * @param $column - Array of column names to display.
 
 * @output A Basic form in where each field is displayed in a seperate line as so:
 *         <field_name>: <input type="text">
 
 * @return - False if an error occurs, True otherwise.
 
 * @include tools/ColumnNames.php
 * 
 * @example of use:
 *     EchoColumnsTable("page.php?table=tablename","submit","Submit",array("f1","f2"))
 * Displays a form for fields f1, f2. 
 * Directs to page.php?table=tablename
 */
function EchoColumnsForm($target, $submit_name, $submit_val, $column) {
    if(!isset($column)) {return;}
    global $echocolumnsform_additionaltext;

    $n = count($column);
    if($n == 0) return false;
    echo "\n<form action='" .$target. "' method='post' class='form'>";
    echo "\n<table>";
    for($i=0; $i<$n; $i++) {
        echo "\n<tr>   <td width=150>".$column[$i]."</td>";
        echo "<td width=120><input type='text' name='".$column[$i]."' size='20'></td></tr>";
    }
    echo "\n</table>";
    echo "\n<input type='submit' name='".$submit_name."' value='".$submit_val."'>";
    echo "</form>";
    return true;
}

/**
 * @param $target - Form's target: 
 *                  <form action='$target'>
 * @param $nitem  - Maximum number of items inputs allowed.
 * @param $before - HTML code to add just after <form>
 * @param $after  - HTML code to add just before </form>. Leave as '' to use default.
 *          Default value = "<br/><input type='submit' name='submit' value='Submit' />"
 *
 * @output A Basic form item / qty form:
 *              $before
 *              ItemID                      qty
 *              <input name="item1">        <input name="qty1">
 *              <input name="item2">        <input name="qty2">
 *                       ....
 *              <input name="item$nitems">  <input name="qty$nitems">
 *              $after
 *
 * @return - False if an error occurs, True otherwise.
 *
 * @include tools/ColumnNames.php
 * 
 * @example of use:
 *     EchoItemListForm("page.php?table=tablename", 10, "<p>Enter Data:</p>", "");
 * Displays a form allowing 10 items maximum to be inputted.
 * Directs to page.php?table=tablename when Submit is clicked.
 */
function EchoItemListForm($target, $nitems, $before, $after) {
    if(!isset($after) OR $after=='') {
        $after = "<br/><input type='submit' name='submit' value='Submit' />";
    }
    echo "\n<form action='$target' method='post'>";
    echo $before;
    echo "\n<table>\n<tr>\n<td width=120>Item ID</td> <td width=80>Quantity</td>\n</tr>";
    for($i=0; $i<$nitems; $i++) {
        echo "<tr> <td width=120><input type='text' name='item".$i."' /></td>".
            "<td width=80> <input type='text' name='qty".$i."' /></td></tr>";
    }
    echo "\n</table>";
    echo $after;
    echo "\n</form>";
}

/**
 * Used by EchoCSVForm(). Not intended for independent usage.
 */
function EchoField($w1, $w2, $label, $name) {
    echo "\n<table cellpadding=0 cellspacing=0><tr height=15>";
    echo "\n<td width=".$w1.">".$label."</td>";
    echo "\n<td width=".$w2."><input type='text' name='".$name."' size='20'></td>";
    echo "\n</tr></table>";
}

/**
 * @param $target - Form's target: 
 *                  <form action='$target'>
 * @param $submit_name, $submit_val - Submit button: 
 *                  <input type='submit' name='$submit_name' value='$submit_val'>
 *
 * @output A Basic file in/output form. Each line represent a table.
 *         Two lines are added at the end for the CSV delimiter / new_line_character
 *
 * @return - False if an error occurs, True otherwise.
 *
 * @include tools/ColumnNames.php
 * 
 * @example of use:
 *     EchoItemListForm("page.php?table=tablename", 10, "<p>Enter Data:</p>", "");
 * Displays a form allowing 10 items maximum to be inputted.
 * Directs to page.php?table=tablename when Submit is clicked.
 */
function EchoCSVForm($target, $submit_name, $submit_value) {
    echo "\n<form action='".$target."' method='post'>";
    EchoField("150","130","Client Data: ","clients");
    EchoField("150","130","Suppliers Data: ","suppliers");
    EchoField("150","130","Items Data: ","items");
    EchoField("150","130","Sales Data: ","sales");
    EchoField("150","130","Sales Data (items): ","itemsSold");
    EchoField("150","130","Purchases Data: ","purchases");
    EchoField("150","130","Purchases Data (items): ","itemsPurchased");
    EchoField("150","130","Facilities Data: ","facilities");
    EchoField("150","130","Stock Data","itemStock");
    echo "<br/>";
    EchoField("150","130","Delimiter: ","dlm");
    EchoField("150","130","New Line character: ","nlc");
    echo "\n<br/><input type='submit' name='".$submit_name."' value='".$submit_value."'>";
    echo "\n</form>\n";
 }

/*
 * Read the array cols and returns the string:
 *   $a[0].", ".$a[1].", ".....", ".$a[n]
 *   where n = count($cols)
 *
 *   If quotes is true then quotation marks will be added for
 *   each element
 *
 * Example:
 *    arrayCommaString(array(a,b,c),false) --> 'a, b, c';
 *    arrayCommaString(array(a,b,c),true)  --> '"a", "b", "c"';
 */
function arrayCommaString($a, $quotes) {
    $n = count($a);
    $first = true;
    $str = '';
    for($i=0; $i<$n; $i++) {
        if(!$first) {$str = $str.', ';}
        if($quotes) {$str = $str.'"'.$a[$i].'"';}
        else        {$str = $str.$a[$i];}
        $first = false;
    }
    return $str;
}

/*
 * Read the array cols and returns the string:
 *   $_POST[ $a[0] ].", ".....", ".$_POST[ $a[n] ]
 *   where n = count($cols)
 *
 * Example:
 *  $_POST['a'] = 1;
 *  $_POST['b'] = 2;
 *  $_POST['c'] = 3;
 *    arrayCommaString(array('a','b','c'),false) --> '1, 2, 3';
 *    arrayCommaString(array('a','b','c'),true)  --> '"1", "2", "3"';
 */
function postArrayCommaString($a, $quotes) {
    $n = count($a);
    $first = true;
    $str = '';
    for($i=0; $i<$n; $i++) {
        if(!$first) {$str = $str.', ';}
        if($quotes) {$str = $str.'"'.$_POST[$a[$i]].'"';}
        else        {$str = $str.$_POST[$a[$i]];}
        $first = false;
    }
    return $str;
}

?>